package contract.jvm;

import org.objectweb.asm.tree.MethodNode;

/**
 * 栈帧
 */
public class Frame {

    /**
     * 局部变量
     */
    public final LocalVariable localVariable;

    /**
     * 操作数栈
     */
    public final OperandStack operandStack = new OperandStack();

    /**
     * 方法
     */
    public final MethodNode methodNode;

    /**
     * 是否返回
     */
    public boolean returned;

    /**
     * 是否空返回
     */
    public boolean returnVoid;

    /**
     * 返回值
     */
    public Object returnValue;

    /**
     * 栈帧构造方法
     */
    public Frame(MethodNode methodNode, Object[] methodArgs) {
        this.methodNode = methodNode;
        if (NativeMethod.isNativeMethod(methodNode)) {
            localVariable = new LocalVariable(methodArgs == null ? 0 : methodArgs.length, methodArgs);
        } else {
            localVariable = new LocalVariable(methodNode.maxLocals, methodArgs);
        }
    }

    /**
     * 设置返回值
     */
    public void returnValue(Object returnValue) {
        this.returned = true;
        this.returnValue = returnValue;
    }

    /**
     * 空返回
     */
    public void returnVoid() {
        this.returned = true;
        this.returnVoid = true;
    }

}
